﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Levels;
using Color = Microsoft.Xna.Framework.Color;

namespace Trippy.Animation
{
    public class Weather : DrawableTrippyComponent
    {
        private Vector2[] particlePositions;
        private float[] particleVelocities;
        private Vector2 rainDirection;
        private float rotation;
        private Vector2 offset;
        private Vector2 scale;
        private Random random;

        public Weather(Level level, float intensity, float minVelocity, float maxVelocity, float direction, Vector2 rainDropScale) : base(level.Game)
        {
            CurrentLevel = level;
            rotation = direction;
            rainDirection = new Vector2((float) Math.Cos(direction), (float) Math.Sin(direction));
            scale = rainDropScale;
            int numParticles = (int) (intensity);
            particlePositions = new Vector2[numParticles];
            particleVelocities = new float[numParticles];
            random = new Random();
            // assign each particle a random position on the screen
            for (int i = 0; i < numParticles; i++)
            {
                particlePositions[i] = new Vector2(random.Next(0, level.Game.GraphicsDevice.Viewport.Width), random.Next(0, level.Game.GraphicsDevice.Viewport.Height));

               
                particleVelocities[i] = (maxVelocity - minVelocity) * (float)random.NextDouble() + minVelocity;
            }
        }

        public new static Texture2D Sprite { get; private set; }
        public static SpriteBatch SpriteBatch { get; private set; } // weather is not affected by camera transformations

        protected override void LoadContent()
        {
            if (Sprite == null) Sprite = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Weather/raindrop");
            if (SpriteBatch == null) SpriteBatch = new SpriteBatch(CurrentLevel.Game.GraphicsDevice);
            offset = new Vector2(Sprite.Width / 2.0f, Sprite.Height / 2.0f);
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < particlePositions.Length; i++)
            {
                particlePositions[i] += rainDirection * particleVelocities[i];
                if (!IsInScreenBounds(particlePositions[i]))
                {
                    if (random.Next(0, 100) < 26)
                    {
                        particlePositions[i] = new Vector2(CurrentLevel.Game.GraphicsDevice.Viewport.Width + 15, random.Next(0, CurrentLevel.Game.GraphicsDevice.Viewport.Height));
                    }
                    else
                    {
                        particlePositions[i] = new Vector2(random.Next(0, CurrentLevel.Game.GraphicsDevice.Viewport.Width), 0);
                    }
                   
                }
            }

            base.Update(gameTime);
        }

        private bool IsInScreenBounds(Vector2 position)
        {
            if (position.X > 0 && position.X < CurrentLevel.Game.GraphicsDevice.Viewport.Width * 3f &&
                position.Y > 0 && position.Y < CurrentLevel.Game.GraphicsDevice.Viewport.Height * 3f)
            {
                return true;
            }
            return false;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Begin();
            for (int i = 0; i < particlePositions.Length; i++)
            {
                //SpriteBatch.Draw(Sprite, particlePositions[i], Color.White);// null, Color.White, rotation, new Vector2(), scale, SpriteEffects.None, 0.0f);
                SpriteBatch.Draw(Sprite, particlePositions[i], null, Color.White, rotation, new Vector2(), scale, SpriteEffects.None, 0f);
            }
            SpriteBatch.End();
            
            base.Draw(gameTime);
        }
    }
}
